package com.track.mapping.tiles;

import java.io.BufferedInputStream;
import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.track.mapview;
import com.track.mapping.content.*;

/**
 * * @author nbukauskas protected
 *
 */
public class Tiles {
	
	private static Tiles _TL;
	private Loader urlLoader = new Loader();
	private SDcard sdLoader = new SDcard();
	
	private static final int bWH = 256; // default tile size
	void onLoad()
	{
		if(_TL != null) return;
		_TL = this;
		
	}
	public static Tiles getInstance()
	{
		return _TL;
		
	}
	/**
	 * 
	 * @return Bitmap loaded from url - SINGLE TILE _ USELESS
	 */ 
	/*
	public Bitmap getTileSetAsBitmap()
	{
		Bitmap bm;
		
		InputStream is;
		
		//BoundingBox box = tile2boundingBox((int)mapview.currentMapLat, (int)mapview.currentMapLon, mapview.currentZoomLevel);
		//Log.w("bounds", ("East :" + box.east+ " North: "+box.north+" West: " + box.west+" South: " + box.south));
		


			 
		is = urlLoader.getURLStream();
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = false;
        opts.inSampleSize = 1; 
        bm =  BitmapFactory.decodeStream(is, null, opts);


		//
        // central tile
        //
       // bm[4] =  BitmapFactory.decodeStream(is, null, opts);
        //return bm[4]; //BitmapFactory.decodeStream(is, null, opts);
		
		
		return bm; //BitmapFactory.decodeStream(is, null, opts);
	}
	*/
/**
 * 
 *  @return - Bitmap of URL based tiles
 * 
 */
	public Bitmap[] getSetOfSlippies(){
		

		Bitmap[] sBm = new Bitmap[9];
		String path = null;
		//is = urlLoader.getURLStreamSet(-1, -1);

		sBm[4] =  BitmapFactory.decodeFile(sdLoader.getSlippyTileStream(0, 0));
		sBm[5] =  BitmapFactory.decodeFile(sdLoader.getSlippyTileStream(0, 1));
		sBm[6] =  BitmapFactory.decodeFile(sdLoader.getSlippyTileStream(0, 1));
		sBm[7] =  BitmapFactory.decodeFile(sdLoader.getSlippyTileStream(0, 1));
		
		
		//tile 1
		//is = urlLoader.getURLStreamSet(0,-1);
		//path = sdLoader.getSlippyTileStream(0, -1);
		//sBm[1] =  BitmapFactory.decodeFile(path);

		/*
		//Tile 2
		//is = urlLoader.getURLStreamSet(1, -1);
		path = sdLoader.getSlippyTileStream(1, -1);
		sBm[2] =  BitmapFactory.decodeFile(path);
		
		
		//Tile 3

		sBm[3] =  BitmapFactory.decodeFile(sdLoader.getSlippyTileStream(-1, 0));
		
		//Tile 4

		sBm[4] =  BitmapFactory.decodeFile(sdLoader.getSlippyTileStream(0,0));

		
		//Tile 5
		
		sBm[5] =  BitmapFactory.decodeFile(sdLoader.getSlippyTileStream(1,0));
	        	        
		//Tile 6
		
		sBm[6] =  BitmapFactory.decodeFile(sdLoader.getSlippyTileStream(-1,1));
		
        
		//Tile 7
		
		sBm[7] =  BitmapFactory.decodeFile(sdLoader.getSlippyTileStream(0,1));
		
		//Tile 8 
		sBm[8] =  BitmapFactory.decodeFile(sdLoader.getSlippyTileStream(1,1));
		*/
		return sBm;
	}
		
	
	/**
	 * 
	 *  
	 * @return Array of Bitmaps to be rendered as a map - THIS is a promissing entry
	 * 
 	 */
	public Bitmap[] getSetOfTiles()
	{
		
		Bitmap[] rBm = new Bitmap[9];
		
		// empty bitmap - flowers to be drawn later
		Bitmap eBm = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
		InputStream is;
		BufferedInputStream bIs;
		
		
		//BoundingBox box = tile2boundingBox((int)mapview.currentMapLat, (int)mapview.currentMapLon, mapview.currentZoomLevel);
		//Log.w("bounds", ("East :" + box.east+ " North: "+box.north+" West: " + box.west+" South: " + box.south));
       
		 BitmapFactory.Options opts = new BitmapFactory.Options();
		 opts.inJustDecodeBounds = false;
		 opts.inSampleSize = 1; 
		
		 
		 //TODO A For() loop needed here
		 // c enter tile
		 

		//tile 0
		is = urlLoader.getURLStreamSet(-1, -1);
		if(is != null){
			
			bIs = new BufferedInputStream(is);
			rBm[0] =  BitmapFactory.decodeStream(bIs, null, opts);
			
		} else {
			rBm[0] = eBm;
			
		}
		
		//tile 1
		is = urlLoader.getURLStreamSet(0,-1);
		if(is != null){
			
			bIs = new BufferedInputStream(is);
			rBm[1] =  BitmapFactory.decodeStream(bIs, null, opts);
		} else {
			rBm[1] = eBm;
			
		}
		//Tile 2
		rBm[2] = eBm;
			
		is = urlLoader.getURLStreamSet(1, -1);
		if(is != null){
			
			bIs = new BufferedInputStream(is);
			rBm[2] =  BitmapFactory.decodeStream(bIs, null, opts);
		} else {
			rBm[2] = eBm;
			
		}
		
		//Tile 3
		is = urlLoader.getURLStreamSet(-1, 0);
		if(is != null){
			
			bIs = new BufferedInputStream(is);
			rBm[3] =  BitmapFactory.decodeStream(bIs, null, opts);
		} else {
			rBm[3] = eBm;
			
		}
		//Tile 4
		is = urlLoader.getURLStreamSet(0, 0);
		if(is != null){
			
			bIs = new BufferedInputStream(is);
			rBm[4] =  BitmapFactory.decodeStream(bIs, null, opts);
		} else {
			rBm[4] = eBm;
		
		
		//Tile 5
		is = urlLoader.getURLStreamSet(1, 0);
		if(is != null){
			
			bIs = new BufferedInputStream(is);
			rBm[5] =  BitmapFactory.decodeStream(bIs, null, opts);
		} else {
			rBm[5] = eBm;
			
		}
	        	        
		//Tile 6
		is = urlLoader.getURLStreamSet(-1, 1);
		if(is != null){
			
			bIs = new BufferedInputStream(is);
			rBm[6] =  BitmapFactory.decodeStream(bIs, null, opts);
		} else {
			rBm[6] = eBm;
			
		}
        
		//Tile 7
		is = urlLoader.getURLStreamSet(0, 1);
		if(is != null){
			
			bIs = new BufferedInputStream(is);
			rBm[7] =  BitmapFactory.decodeStream(bIs, null, opts);
		} else {
			rBm[7] = eBm;
			
		}
		//Tile 8 
		is = urlLoader.getURLStreamSet(1, 1);
		if(is != null){
			
			bIs = new BufferedInputStream(is);
			rBm[8] =  BitmapFactory.decodeStream(bIs, null, opts);
		} else {
			rBm[8] = eBm;
			
		}
		
		
		//is = null;
		
		
		//return rBm;
		
		 // FAKE set!
		 /*
		 rBm[0]= eBm;
		 rBm[1]= eBm;
		 rBm[2]= eBm;
		 rBm[3]= eBm;
		 rBm[4]= eBm;
		 rBm[5]= eBm;
		 rBm[6]= eBm;
		 rBm[7]= eBm;
		 rBm[8]= eBm;
		 */
		 
		
		
	}
		return rBm;
}
	
	
	/**
	 * 
	 * 
	 *  Class to handle the bunding box
	 * @author nbukauskas
	 *
	 */
	 private class BoundingBox {
	    
		double north = 1;
		double northEast = 1;
		double northWest = 1;
	    double south =1;
	    double southEast = 1;
	    double southWest = 1;
	    double east =1;
	    double west = 1;
	    double centerLat = 1;
	    double centerLon = 1;
	    
	    
	  }
		 BoundingBox tile2boundingBox(final int x, final int y, final int zoom) {
			 
		    BoundingBox bb = new BoundingBox();
		    bb.centerLat = x;
		    bb.centerLon = y;
		    bb.north = tile2lat(y, zoom);
		    bb.south = tile2lat(y + 1, zoom);
		    bb.west = tile2lon(x, zoom);
		    bb.east = tile2lon(x + 1, zoom);
		    
		    return bb;
		  }

	  static double tile2lon(int x, int z) {
	     return (x / Math.pow(2.0, z) * 360.0) - 180;
	  }

	  static double tile2lat(int y, int z) {
	    double n = Math.PI - ((2.0 * Math.PI * y) / Math.pow(2.0, z));
	    return 180.0 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));
	  }
}
